#include <stdio.h>
#include <malloc.h>
#include <assert.h>

int Get_number_of_char(FILE *);
int min_int(int a, int b);

int main()
{
	FILE *onegin = 0, *sonegin = 0;
	int *text = 0, **starts_of_strings = 0, *l = 0;
	int i = 0, j = 0, k = 0, m = 0, number_of_char = 0, number_of_bn = 0, code_of_char1 = 0, code_of_char2 = 0, length_1 = 0, length_2 = 0;
	char c = 0;

	//opening of input file
	onegin = fopen("onegin very big.txt", "r");
	assert(onegin != NULL);

	//opening of output file
	sonegin = fopen("sorted onegin.txt", "w");
	assert(onegin != NULL);

	//calculating number of chars in file
	number_of_char = Get_number_of_char(onegin);
	printf("Number of chars in file: %d\n", number_of_char);

	//allocating dynamic memory
	text = (int*)calloc(number_of_char + 1, sizeof (int));
	printf("allocating dynamic memory completed\n");

	//coping text from file to array
	rewind(onegin);
	for (i = 0; i < number_of_char; i++)
	{
		text[i] = fgetc(onegin);

		//putchar(text[i]);
	}
	printf("coping text from file to array completed\n");

	//searching for "\n"
	//calculating number of \n
	number_of_bn = 0;
	for (i = 0; i < number_of_char; i++)
	{
		if (text[i] == '\n')
			number_of_bn++;
	}
	printf("calculating number of \\n completed\n");

	//allocating dynamic memory
	starts_of_strings = (int**)calloc(number_of_bn, sizeof(int*));

	//first pointer should point to the very beginning of file
	j = 0;
	starts_of_strings[j] = &(text[0]);

	//making the array of pointers to the beginnings of the strings
	j = 1;
	for (i = 0; i < number_of_char; i++)
	{
		if (text[i] == '\n')
		{
			starts_of_strings[j] = &text[i + 1];
			assert(&text[i + 1] != NULL);
			j++;

			//printf ("Number of \\n: %d \n", j);
		}
	}

	printf("\nFirst char in the first line: %c", **(starts_of_strings));

	printf("\nThird char in the second line: %c\n\n", starts_of_strings[1][2]);

	//comparing of strings
	printf("Exact number of \\n: %d\n", number_of_bn);

	//doing the baloon sorting
	for (m = 0; m < number_of_bn; m++)
	{
		for (i = 0; i < number_of_bn /*+ 1*/ - m - 1; i++)
		{
			k = 0;
			while (1)
			{
				code_of_char1 = (int)starts_of_strings[i][k];
				//printf ("code_of_char1: %c\n", code_of_char1);

				code_of_char2 = (int)starts_of_strings[i + 1][k];
				//printf ("code_of_char2: %c\n", code_of_char1);



				//printf("%c ", starts_of_strings[i][k]); printf("%c \n", starts_of_strings[i + 1][k]);

				if (code_of_char1 != code_of_char2)
				{
					if (code_of_char1 > code_of_char2)
					{
						l = starts_of_strings[i];
						starts_of_strings[i] = starts_of_strings[i + 1];
						starts_of_strings[i + 1] = l;

						//printf("omg");

						break;
					}
					else
					{
						break;
					}
				}
				else /*break;*/if ((code_of_char1 == '\n') && (code_of_char2 == '\n'))
				{
					break;
				}
				k++;
			}
		}
	}

	//printing new sorted text
	printf("\n");
	for (j = 0; j < number_of_bn; j++)
	{
		for (i = 0; starts_of_strings[j][i] != '\n'; i++)
		{
			printf("%c", starts_of_strings[j][i]);
			k = fputc(starts_of_strings[j][i], sonegin);
			assert(k != EOF);
		}
		printf("\n");
		k = fputc('\n', sonegin);
	}

	//closing opened files
	i = fclose(onegin);
	assert(i == 0);

	i = fclose(sonegin);
	assert(i == 0);

	//giving freedom to all dynamic memory
	free(text);
	//free(starts_of_strings);    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


	return 0;
}


//function that calculates the number of chars in the file
int Get_number_of_char(FILE *text)
{
	int c = 0, i = 0, number_of_char = 0;

	while ((c = fgetc(text)) != EOF)
	{
		number_of_char++;
		i++;
	}

	return number_of_char;
}

//function that returns min of two int
int min_int(int a, int b)
{
	int help = 0;
	if (a > b)
	{
		help = a;
		a = b;
		b = help;
	}
	return a;
}
